JS 运算符

一、 运算操作符 +
1.数学运算，字符串连接
2.任何数据类型加字符串都等于字符串

<script type="text/javascript">
	var a = 1 + 2;  //加号优先级大于等号
	console.log(a); 

	var a = "a" + "b";
	console.log(a); //字符串相加，直接头尾连接

	var b = 1 + 1 + "a" + true; //运算从左向右
	console.log(b); 
</script>

二、 -  *  /  %  =  ()

<script type="text/javascript">
	var a = 1 / 0; //infinity Number 无穷也算数，数字的数据类型
	console.log(a);
	var a = -1 / 0;// -infinity
	console.log(a);
	var a = 0 / 0; // NaN Not a Number 
	console.log(a);
</script>

<script type="text/javascript">
	var a = 1 / 5; //余数
	console.log(a);
	var a = 10 / 5;
	console.log(a);
</script>

括号优先级最高
<script type="text/javascript">
	var a = 1 + "a" + (1 + 1);
	console.log(a);
</script>

三、 ++
<script type="text/javascript">
	var a = 1;
	a = a + 1;
	console.log(a);

	var a = 1;
	a ++; //等效于 a = a + 1;
	console.log(a);
	console.log(a ++); // 先打印结果再执行++
	console.log(a);
	console.log(++ a);//先执行++再打印
</script>

<script type="text/javascript">
	var a = 1;
	var b = a ++ + 1;//a等语句执行完再++，先a + 1,再赋值给b，最后a ++
	console.log(a);//a ++ 后的结果
	console.log(b);

	var a = 1;
	var b = ++ a + 1; //先++ a 再执行语句
	console.log(a);
	console.log(b);
</script>

--
a --
a = a - 1; 

<script type="text/javascript">
	var a = 1;
	var b = a -- + -- a;
	console.log(b);
	console.log(a);
</script>

<script type="text/javascript">
	var a = 1;
	var b = -- a + -- a;
	console.log(b);
	console.log(a);
</script>


四、 += *= %= 
<script type="text/javascript">
	var a = 1;
	a += 10; // a = a + 10;
	console.log(a)

	var a = 1;
	a += 10 + 1;//a = a + 10 + 1;

	var a = 1;
	a %= 10;
	console.log(a);
</script>


ex:
<script type="text/javascript">
	var a = (10 * 3 - 4 / 2 + 1) % 2,
		b = 3; 
	b %= a + 3;
	console.log(a ++);
	console.log(-- b);
</script>

var a = 123; var b =234;请交换a,b 的值

<script type="text/javascript">
	var a = 123;
	var b = 234;

	var c = a;
	var a = b;
	var b = c;
	console.log(a,b)

	var a = 123;
	var b = 234;

	a = a + b;
	b = a - b;
	a = a - b;
	console.log(a,b)
</script> 

五、 比较运算符
< > == <= >= != 
字符串比较,比较在ASC码中的顺序
<script type="text/javascript">
	var a = "a" > "b";//比较a和b在ASC码中的位置
	console.log(a); 

	var a = "1" > "8"; 
	console.log(a);

	var a = "10" > "7"; //比较第一位，再比较第二位
	console.log(a);
</script>

<script type="text/javascript">
	var a = 1 == 2;// 1是否等于2
	console.log(a);

	var a = 1 != 2;// 1是否不等于2
	console.log(b);

	var a = 1 == 1;
	console.log(a);

	var a = undefined == undefined;
	console.log(a);

	var a = infinity == infinity;
	console.log(a);

	var a = NaN == NaN;
	console.log(a);
</script>

六、 逻辑运算符
&&    ||   ！

被认定为false的值
undefined  null	NaN "" 0 false 

&& 与运算符
<script type="text/javascript">
	var a = 1 && 1 && 1;// 遇到真继续读
	console.log(a);

	var a = 1 + 1 && 1 - 1;
	console.log(a);

	0 && document.write(a); //  && 作为中断作用，0转为布尔值false后，输出false，不读&&右边
	2 > 1 && console.log("能够打印"); //&& 左边转化为布尔值是ture,执行右边 

	var data = ...;
	data && 执行一个语句，会用到data ; // 如果data为空则不执行 &&右边，保证语句：保证data有数据才执行用到data的语句
</script>

与运算符& 
<script type="text/javascript">
	var a = 1 & 3;
	console.log(a);
	// 1和3转化为二进制的01,11
	// 01
	// 11
	// --
	// 01
	// 与运算，相同为1，不同为0
</script> 

|| 或运算符
<script type="text/javascript">
	var a = 1 || 2; //碰到真返回值
	console.log(a);

	var a = 0 || 3;//碰到假找真
	console.log(a);

	var a = 0 || false;//碰到假，返还第二值
	console.log(a);

	var a = 0 || false || 1;
	console.log(a);
</script>

<script type="text/javascript">
	div.onclick = function e() {
		var event = e;
		//IE浏览器 ,e为undefined,只有window.event;
		var event = e || window.event; // 非IE浏览器e有值，直接返回；IE浏览器执行e没值，e的Undefin转化布尔值为假，有window.event,返回window.event
	}
</script>

<script type="text/javascript">
	var a = !"";//转布尔值，再取反
	console.log(a);

	var a = !!""; //两次取反
	console.log(a);

	var a = true;
	a = !a;
	console.log(a);

	var a = true;
	a != a;//左右等不等，是否不等于
	console.log(a);
</script>

<script type="text/javascript">
	//window.foo的值
	window.foo || (window.foo = 'bar');//先读括号里，window.foo = 'bar'
</script>

逗号运算符
<script type="text/javascript">
	var a = (1,2 + 2);//先计算逗号前面，再计算逗号后面，再把第二个值返回给a
	console.log(a)
</script>